package com.xyz.rok.tools.controller;

import com.alibaba.excel.EasyExcel;
import com.google.common.base.CaseFormat;
import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.model.dto.StatDto;
import com.xyz.rok.tools.model.request.DkpStatRequest;
import com.xyz.rok.tools.model.request.StatRequest;
import com.xyz.rok.tools.model.response.StatDownloadDto;
import com.xyz.rok.tools.service.DkpService;
import com.xyz.rok.tools.service.MemberStatService;
import com.xyz.rok.tools.service.PlayerStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/5/12 15:34
 **/
@Slf4j
@RestController
@RequestMapping("/api/stat")
public class StatController {

    @Autowired
    private MemberStatService memberStatService;
    @Autowired
    private PlayerStatService playerStatService;
    @Autowired
    private DkpService dkpService;

    @PostMapping("/addStat")
    public Result addStat(@RequestBody DkpStatRequest dkpStatRequest) {
        playerStatService.insertOrUpdate(dkpStatRequest);
        return Result.success();
    }

    @GetMapping("/selectBySerialNumber")
    @ResponseBody
    public Result selectBySerialNumber(@RequestParam("serialNumber") String serialNumber) throws Exception {
        return Result.success(dkpService.selectBySerialNumber(serialNumber));
    }

    @GetMapping("/getFinishedNames")
    public Result getFinishedNames(@RequestParam("serverId") Integer serverId) {
        return Result.success(playerStatService.getFinishedNames(serverId));
    }

    @PostMapping("/getStatCompare")
    public Result getStatCompare(@RequestBody StatRequest statRequest) throws Exception {
        List<StatDto> list = playerStatService.getStatCompare(statRequest.getServerId(), statRequest.getBeginDate(), statRequest.getEndDate(), CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, statRequest.getType()));
        return Result.success(list);
    }

    @PostMapping("/getDKPCompare")
    public Result getDKPCompare(@RequestBody StatRequest statRequest) throws Exception {
        List<StatDto> list = playerStatService.getDKPCompare(statRequest.getServerId(), statRequest.getBeginDate(), statRequest.getEndDate(), statRequest.getT4Kill(), statRequest.getT5Kill(), statRequest.getDeath());
        return Result.success(list);
    }

    @PostMapping("/getStatDate")
    public Result getMemberStatDate(@RequestBody StatRequest statRequest) {
        List<StatDto> list = playerStatService.getStatDate(statRequest.getServerId(), statRequest.getDay(), CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, statRequest.getType()));
        return Result.success(list);
    }

    @GetMapping("/getHasDataDay")
    public Result getHasDataDay(@RequestParam("serverId") Integer serverId) {
        return Result.success(playerStatService.getHasDataDay(serverId));
    }

    @GetMapping("/download")
    public void download(@RequestParam("serverId") Integer serverId, @RequestParam("day") Integer day, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = day.toString();
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<StatDto> memberStatDtoList = playerStatService.selectStatDownloadByDay(serverId, day);
        List<StatDownloadDto> statDownloadDtoList = new ArrayList<>();
        StatDownloadDto statDownloadDto = null;
        for (StatDto memberStatDto : memberStatDtoList) {
            statDownloadDto = new StatDownloadDto();
            BeanUtils.copyProperties(memberStatDto, statDownloadDto);
            statDownloadDtoList.add(statDownloadDto);
        }
        EasyExcel.write(response.getOutputStream(), StatDownloadDto.class).sheet(day).doWrite(statDownloadDtoList);
    }

    @PostMapping("/checkingDKP")
    @ResponseBody
    public Result checkingTitle(@RequestBody DkpStatRequest dkpStatRequest) throws Exception {
        dkpService.checkingDkp(dkpStatRequest);
        return Result.success();
    }

}
